/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.portal.api.service.impl;

import com.channelsharing.common.cache.CacheDuration;
import com.channelsharing.common.cache.ExpireTimeConstant;
import com.channelsharing.common.service.CrudServiceImpl;
import com.channelsharing.hongqu.portal.api.constant.Constant;
import com.channelsharing.hongqu.portal.api.dao.OrderCommentDao;
import com.channelsharing.hongqu.portal.api.entity.GoodsInfo;
import com.channelsharing.hongqu.portal.api.entity.OrderComment;
import com.channelsharing.hongqu.portal.api.entity.OrderGoods;
import com.channelsharing.hongqu.portal.api.enums.OrderCommentStatus;
import com.channelsharing.hongqu.portal.api.enums.OrderCommentType;
import com.channelsharing.hongqu.portal.api.service.GoodsInfoService;
import com.channelsharing.hongqu.portal.api.service.OrderCommentService;
import com.channelsharing.hongqu.portal.api.service.OrderGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 评价接口Service
 *
 * @author liuhangjun
 * @version 2018-06-06
 */
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
@Service
public class OrderCommentServiceImpl extends CrudServiceImpl<OrderCommentDao, OrderComment> implements OrderCommentService {


    public static final String PORTAL_CACHE_PREFIX = Constant.PORTAL_CACHE_PREFIX;
    @Autowired
    private OrderGoodsService orderGoodsService;
    @Autowired
    private GoodsInfoService goodsInfoService;

    /**
     * @param id
     * @return
     */
    @Override
    public OrderComment findOne(Long id) {
        OrderComment entity = new OrderComment();
        entity.setId(id);
        OrderComment orderComment = super.findOne(entity);
        return orderComment;
    }

    /**
     * 查询单个商品时，加上详情内容
     *
     * @param entity
     * @return
     */
    @Override
    public OrderComment findOne(@NotNull OrderComment entity) {

        OrderComment orderComment = super.findOne(entity);
        if (orderComment == null) {
            return new OrderComment();
        }
        return orderComment;
    }

    @Override
    @Transactional
    public void add(OrderComment entity) {
        super.add(entity);
        //更新商品好评率
        GoodsInfo goodsInfo = goodsInfoService.findOne(entity.getGoodsId());
        if (goodsInfo != null) {
            Integer currentRate = goodsInfo.getCommentRate();
            double totalScore = currentRate * goodsInfo.getCommentNumber();
            switch (entity.getStar()) {
                case 5:
                    totalScore = totalScore + 100;
                    break;
                case 4:
                    totalScore = totalScore + 90;
                    break;
                case 3:
                    totalScore = totalScore + 85;
                    break;
                case 2:
                    totalScore = totalScore + 75;
                    break;
                case 1:
                    totalScore = totalScore + 60;
                    break;
            }
            Double newRateD = totalScore / (goodsInfo.getCommentNumber() + 1);
            Integer integer = newRateD.intValue();
            goodsInfoService.updateCommentRate(goodsInfo.getId(),integer);
            OrderGoods orderGoods=orderGoodsService.findOne(entity.getOrderGoodsId());
            if(orderGoods!=null){
                orderGoods.setCommentStatus(OrderCommentStatus.yes.getCode());
                orderGoodsService.modify(orderGoods);
            }
        }
    }
}
